package com.service.impl;

import com.aop.MyAspect;
import com.dao.SysLogDao;
import com.domain.PageObject;
import com.domain.sys.SysLog;
import com.exception.ServiceException;
import com.service.SysLogService;
import com.util.MyValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;

@Service

public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;
    @Override
    /**
     * name 要查询的名字
     * pageCurrent 当前页码值
     */
    @MyAspect("日志查询")
    public PageObject<SysLog> findPageObjects(String name, Integer pageCurrent) {
        MyValid.isIll(pageCurrent == null || pageCurrent < 1,"当前页码不正确");


        //基于 name 查询匹配的总记录
        int rowCount=sysLogDao.getRowCount(name);
        //2.2) 验证查询结果，假如结果为0不再执行如下操作
        MyValid.isService(rowCount==0,"系统没有查到对应记录");


        int pageSize = 10;
        int starIndex = (pageCurrent - 1) * pageSize;
        ArrayList<SysLog> list = sysLogDao.findPageObjects(name,starIndex,pageSize);



        PageObject<SysLog> pageObject = new PageObject<>();


        //当前页面的起始值
        pageObject.setPageCurrent(pageCurrent);
        //页面大小
        pageObject.setPageSize(pageSize);
        //总行数
        pageObject.setRowCount(rowCount);
        //存放每一行记录
        pageObject.setRecords(list);
        //总页数
        pageObject.setPageCount((rowCount-1) / pageSize+1);

        return pageObject;
    }

    @MyAspect("日志删除")
    @Override
    public Integer deleteObjects(Integer... ids) {
        //1.判定参数合法性
        if(ids==null||ids.length==0)
            throw new IllegalArgumentException("请选择一个");
        //2.执行删除操作
        int rows;
        try{
            rows=sysLogDao.deleteObjects(ids);
        }catch(Throwable e){
            e.printStackTrace();
            //发出报警信息(例如给运维人员发短信)
            throw new ServiceException("系统故障，正在恢复中...");
        }
        //4.对结果进行验证
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        //5.返回结果
        return rows;

    }





    @Override
    public void saveObject(SysLog entity) {
        sysLogDao.insertObject(entity);
    }




}
